<template>
	<view class="update-mask flex-center">
		<view class="content botton-radius">
			<view class="content-top">
				<view class="content-top-text">
					<text class="">发现新版本 v{{data.data.edition_name}}</text>
					<text class="version">当前版本：{{data.version}}</text>
				</view>
				<image class="content-top" style="top: 0;" width="100%" height="100%" src="/static/update/bg_top.png">
				</image>
			</view>
			<view class="content-header"></view>
			<view class="content-body">

				<view class="title"><text>更新内容</text></view>
				<view class="body">
					<scroll-view class="box-des-scroll" scroll-y="true">
						<view class="whitespace-pre-wrap">
							<rich-text :nodes="data.data.describe" style="font-size: 12px;"></rich-text>
						</view>
					</scroll-view>
				</view>
				<view class="footer flex-center">
					<view class="progress-box flex-column" v-if="!data.updateBtn">
						<u-line-progress class="progress" :percentage="data.percent" height="8"
							activeColor="#3DA7FF"></u-line-progress>
						<view><text class="fs24">正在下载，请稍后 ({{data.downloadedSize}}/{{data.packageFileSize}}M)</text>
						</view>
					</view>

					<button class="content-button" style="border: none;color: #fff;" plain @click="confirm"
						v-if="data.updateBtn">立即升级</button>
				</view>
			</view>

			<image v-if="data.cancleBtn" class="close-img" src="/static/update/app_update_close.png"
				@click.stop="cancel"></image>
		</view>
	</view>
</template>

<script setup>
	import {
		onMounted,
		reactive,
		watch
	} from "vue"
	const props = defineProps({
		data: {
			type: Object
		}
	})
	const data = reactive(props.data)
	const emits = defineEmits(["cancel", "downloadComplete"])
	// const data = reactive({
	// 	version: '0.0.0', //当前运行版本(打包时manifest里的版本名称)
	// 	percent: 0, //进度条百分比
	// 	updateBtn: true, //是否显示立即更新
	// 	cancleBtn: true, //是否显示取消按钮
	// 	downloadedSize: 0, //当前已下载大小
	// 	packageFileSize: 0, //安装包大小
	// 	data: {
	// 		describe: '',
	// 		edition_url: '', //安装包下载地址或者通用应用市场地址
	// 		edition_force: 0, //是否强制更新 0代表否 1代表是
	// 		package_type: 0, //0是整包升级 1是wgt升级
	// 		edition_name: '0.0.0' //后端返回的版本名称
	// 	}
	// })


	// onLoad(({
	// 	obj
	// }) => {
	// 	if (obj) {
	// 		data.data = JSON.parse(obj);
	// 		if (data.data.edition_force == 0) {
	// 			data.cancleBtn = true;
	// 		}
	// 	}

	// 	plus.runtime.getProperty(plus.runtime.appid, (inf) => {
	// 		data.version = inf.version;
	// 	})
	// })

	watch(
		() => data.percent,
		(percent) => {
			if(percent === 100) {
				emits("downloadComplete")
			}
		})
	const cancel = () => {
		emits("cancel")
	}
	// 比较版本号
	function compareVersion(v1, v2) {
		const arr1 = v1.split('.')
		const arr2 = v2.split('.')
		if (arr1.length === arr2.length) {
			for (let i = 0; i < arr1.length; i++) {
				let ver1 = parseInt(arr1[i] || '0')
				let ver2 = parseInt(arr2[i] || '0')
				if (ver1 > ver2) {
					return 1
				} else if (ver1 < ver2) {
					return -1
				}
			}
			return 0
		} else {
			data.$showMessage('版本号的长度不一致，请联系管理员解决')
			return 0
		}
	}


	function confirm() {
		if (data.data.package_type == 0) {
			//apk整包升级 下载地址必须以.apk结尾
			if (data.data.edition_url.includes('.apk')) {
				data.updateBtn = false;
				data.cancleBtn = false;
				download();
			} else {
				//外部下载 一般是手机应用市场或者其他h5页面
				data.data.edition_force = 0; // 解决跳转外部链接后，更新提示还在的问题
				plus.runtime.openURL(data.data.edition_url);
				uni.navigateBack({
					delta: 1
				});
			}
		} else {
			data.updateBtn = false;
			data.cancleBtn = false;
			//wgt资源包升级 下载地址必须以.wgt结尾
			download();
		}
	}

	function download() {
		let package_type = data.data.package_type;
		let that = this;
		const downloadTask = uni.downloadFile({
			url: data.data.edition_url,
			success: res => {
				if (res.statusCode === 200) {
					plus.runtime.install(
						res.tempFilePath, {
							force: true //true表示强制安装，不进行版本号的校验；false则需要版本号校验，
						},
						function() {
							// console.log('success', success);
							if (package_type == 1) {
								plus.runtime.restart();
							}
						},
						function(e) {
							//提示部分wgt包无法安装的问题
							that.data.edition_force = 0;
							uni.showToast({
								title: e.message,
								icon: 'none',
								duration: 2500
							})
							setTimeout(() => {
								uni.navigateBack()
							}, 2000)

						}
					);
					if (package_type == 0) {
						// 解决安装app点击取消，更新还在的问题
						data.data.edition_force = 0;
						uni.navigateBack();
					}
				}
			}
		});
		// 进度条
		downloadTask.onProgressUpdate(res => {
			data.percent = res.progress;
			data.downloadedSize = (res.totalBytesWritten / Math.pow(1024, 2)).toFixed(2);
			data.packageFileSize = (res.totalBytesExpectedToWrite / Math.pow(1024, 2)).toFixed(2);
		});

	}
</script>

<style>
	/* page {
		background: transparent;
	} */

	.flex-center {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		justify-content: center;
		align-items: center;
	}

	.update-mask {
		position: fixed;
		left: 0;
		top: 0;
		right: 0;
		bottom: 0;
		background-color: rgba(0, 0, 0, 0.65);
	}

	.botton-radius {
		border-bottom-left-radius: 30rpx;
		border-bottom-right-radius: 30rpx;
	}

	.content {
		position: relative;
		top: 0;
		width: 600rpx;
		background-color: #fff;
		box-sizing: border-box;
		padding: 0 50rpx;
		font-family: Source Han Sans CN;
	}

	.text {
		/* #ifndef APP-NVUE */
		display: block;
		/* #endif */
		line-height: 200px;
		text-align: center;
		color: #ffffff;
	}

	.content-top {
		position: absolute;
		top: -195rpx;
		left: 0;
		width: 600rpx;
		height: 270rpx;
	}

	.content-top-text {
		font-size: 40rpx;
		font-weight: bold;
		color: #f8f8fa;
		position: absolute;
		top: 120rpx;
		left: 50rpx;
		z-index: 1;
		display: flex;
		flex-direction: column;
	}

	.content-header {
		height: 70rpx;
	}

	.title {
		font-size: 33rpx;
		font-weight: bold;
		color: #3da7ff;
		line-height: 38px;
	}

	.footer {
		height: 150rpx;
		display: flex;
		align-items: center;
		justify-content: space-around;
	}

	.box-des-scroll {
		box-sizing: border-box;
		padding: 0 40rpx;
		text-align: left;
	}

	.box-des {
		font-size: 26rpx;
		color: #000000;
		line-height: 50rpx;
	}

	.progress-box {
		width: 100%;
	}

	.progress {
		width: 83%;
		/* height: 40rpx; */
		border-radius: 35px;
	}

	.close-img {
		width: 70rpx;
		height: 70rpx;
		z-index: 1000;
		position: absolute;
		bottom: -120rpx;
		left: calc(50% - 70rpx / 2);
	}

	.content-button {
		text-align: center;
		flex: 1;
		font-size: 30rpx;
		font-weight: 400;
		color: #ffffff;
		border-radius: 40rpx;
		margin: 0 18rpx;

		height: 80rpx;
		line-height: 80rpx;

		background: linear-gradient(to right, #1785ff, #3da7ff);
	}

	.flex-column {
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.fs24 {
		font-size: 24rpx;
	}

	.version {
		font-size: 24rpx;
		margin-top: 10rpx;
		color: #eeeeee;
		text-decoration: underline;
	}
</style>